<?php
/**
 * Geneone: Content Management Framework
 * Login
 *
 * http://geneone.neondragon.net/
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2004-2006 Geneone Development Team
 */
 
class Gene_Special_Login {
	/**
	 * Default Action
	 */
	function main() {
		$auth =& Gene::getAuth();
		$tpl =& Gene::getTemplate();
		$tpl->assign ("pagetitle", "Sign In");
		
		/* Already logged in users */
		if ($auth->loggedIn()) {
			header ("Location: ".Gene::makeLink(Gene::getSetting("default_obj")));
			exit();
		}
		
		// What's our throttle status?
		$throttle = $auth->getThrottleLevel();

		if ($throttle == GENE_AUTH_THROTTLE_BLOCKED) {
			$tpl->assign ("permissionerror", "Logins from your IP address have been temporarily blocked due to abnormal usage. Please try again later.");
			$tpl->assign("finalbody", $tpl->fetch("errors/permissionerror"));
			return;
		} else if ($throttle == GENE_AUTH_THROTTLE_CAPTCHA) {
			$tpl->assign ("captcha", true);
		}
		
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			if ($throttle == GENE_AUTH_THROTTLE_CAPTCHA) {
				$captchaid = md5(Gene_Request::getString("captchaid", GENE_REQUEST_POST));
				$captchacode = Gene_Request::getString("captchacode", GENE_REQUEST_POST);
				if (isset($_SESSION['captcha_object']) && isset($_SESSION['captcha_object'][$captchaid]) && $_SESSION['captcha_object'][$captchaid] == $captchacode) {
					$captchaerror = false;
				} else {
					$captchaerror = true;
				}
				unset($_SESSION['captcha_object'][$captchaid]);
			}
			
			if ($throttle == GENE_AUTH_THROTTLE_CAPTCHA && $captchaerror) {
				$tpl->assign ("errormsg", "You did not enter the code correctly.");
			} else {
				$loginname = Gene_Request::getString("loginname", GENE_REQUEST_POST);
				$password = Gene_Request::getString("loginpassword", GENE_REQUEST_POST);
				$remember = Gene_Request::getInteger("loginremember", GENE_REQUEST_POST);
				if ($remember == 1) {
					$persistant = true;
				} else {
					$persistant = false;
				}
				
				$user = Gene_User::fromPublicName($loginname);
				if ($throttle == GENE_AUTH_THROTTLE_NONE && $user) {
					// Per user login throttling
					$userId = $user->getUserId();
					if ($auth->getUserThrottleLevel($userId) == GENE_AUTH_THROTTLE_CAPTCHA) {
						$tpl->assign ("captcha", true);
						
						$captchaid = md5(Gene_Request::getString("captchaid", GENE_REQUEST_POST));
						$captchacode = Gene_Request::getString("captchacode", GENE_REQUEST_POST);
						if (isset($_SESSION['captcha_object']) && isset($_SESSION['captcha_object'][$captchaid])) {
							if ($_SESSION['captcha_object'][$captchaid] == $captchacode) {
								$captchaerror = false;
							} else {
								$captchaerror = true;
							}
							unset($_SESSION['captcha_object'][$captchaid]);
						} else {
							$captchaerror = true;
						}
					}
				}
				
				if (isset($captchaerror) && $captchaerror) {
					if (!Gene_Request::getBoolean("captchaid", GENE_REQUEST_POST)) {
						$tpl->assign ("missingusercaptcha", true);
					} else {
						$tpl->assign ("errormsg", "You did not enter the code correctly.");
					}
				} else {
					$login = $auth->doLogin($loginname, $password, $persistant);
					if (!PEAR::isError($login)) {
						if ($remember == 1) {
							Gene_Request::setCookie("loginname", $loginname); /* Set 360 day cookie */
						} else if ($remember == 2) {
							Gene_Request::setCookie("loginname", $loginname); /* Set 360 day cookie */
						} else {
							Gene_Request::setCookie("loginname", ""); /* Remove cookie */
						}
						
						if ($r = Gene_Request::getString("r", GENE_REQUEST_GET | GENE_REQUEST_POST)) {
							header ("Location: ".Gene::makeLink($r));
						} else {
							header ("Location: ".Gene::makeLink(Gene::getSetting("default_obj")));
						}
						
						exit();
					} else {
						if ($auth->getThrottleLevel($auth->getThrottleCount()+1)) {
							$tpl->assign ("captcha", true);
						}
						
						$tpl->assign ("errormsg", $login->message);
					}
				}
			}
		} else {
			if ($status = $auth->getStatus()) {
				switch ($status) {
					case AUTH_IDLED:
						$status = "Your current login is idle.";
						break;
					case AUTH_EXPIRED:
						$status = "Your current login has expired.";
						break;
					case AUTH_WRONG_LOGIN:
						$status = "Incorrect login details. Please check for typos, and whether your Caps Lock key is on.";
						break;
				}
				$tpl->assign ("errormsg", $status);
			}
		}
	
		if (Gene_Request::getString("loginname", GENE_REQUEST_COOKIE)) {
			$tpl->assign("loginname", Gene_Request::getString("loginname", GENE_REQUEST_COOKIE));
		}
		
		if ($r = Gene_Request::getString("r", GENE_REQUEST_GET | GENE_REQUEST_POST)) {
			$tpl->assign("return", $r);
		}
		
		$tpl->assign("finalbody", $tpl->fetch("special/login"));
	}
}

?>